package com.example.administrator.videotest2.util;

import android.content.Context;
import android.text.TextUtils;

import com.example.administrator.videotest2.entity.Banner1;
import com.example.administrator.videotest2.entity.DS360_dt;
import com.example.administrator.videotest2.entity.VideoCover;
import com.example.administrator.videotest2.entity.Yun_Video;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class JsonUtil {

    //处理服务器返回来的json数据
public static ResponseJson<VideoCover> handle_cover(String response){
    ResponseJson<VideoCover> responseJson=new ResponseJson<>();
    if (!TextUtils.isEmpty(response)){
        try {
            Gson gson=new Gson();

            List<VideoCover>list= gson.fromJson(response, new TypeToken<List<VideoCover>>() {}.getType());

           // Log.i("pp22",list.toString());
            responseJson.setCode(true);
            responseJson.setDataList(list);

        } catch (Exception e) {

        }
    }
    return responseJson;
}

    public static List<Banner1> handle_banner(String response){
        List<Banner1>list=new ArrayList<>();
        if (!TextUtils.isEmpty(response)){
            try {
                Gson gson=new Gson();

                list= gson.fromJson(response, new TypeToken<List<Banner1>>() {}.getType());

                // Log.i("pp22",list.toString());

            } catch (Exception e) {

            }
        }
        return list;
    }

    public static DS360_dt handle_video(String response){
        DS360_dt dt=null;
        if (!TextUtils.isEmpty(response)){
            try {
                Gson gson=new Gson();
                dt= gson.fromJson(response, DS360_dt.class);

            } catch (Exception e) {

            }
        }
        return dt;
    }

    public static Yun_Video yun_video(String response){
        Yun_Video dt=null;
        if (!TextUtils.isEmpty(response)){
            try {
                Gson gson=new Gson();
                dt= gson.fromJson(response, Yun_Video.class);

            } catch (Exception e) {

            }
        }
        return dt;
    }

    public static String ReadFile(String Path){
        BufferedReader reader = null;
        String laststr = "";
        try{
            FileInputStream fileInputStream = new FileInputStream(Path);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
            reader = new BufferedReader(inputStreamReader);
            String tempString = null;
            while((tempString = reader.readLine()) != null){
                laststr += tempString;
            }
            reader.close();
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return laststr;
    }

    public static String getJson(String fileName, Context context){
        StringBuilder stringBuilder = new StringBuilder();
        try {
            InputStream is = context.getAssets().open(fileName);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line=bufferedReader.readLine()) != null){
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return stringBuilder.toString();
    }
}